package net.yihang.service;

import net.yihang.mapper.PersonMapper;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PersonService {

    private PersonMapper personMapper;

    public PersonService(PersonMapper personMapper) {
        this.personMapper = personMapper;
    }

    /**
     * 练习3
     * 按性别分组，返回柱状图 bar 所需数据
     *
     * @return 要求返回的数据格式为 {"sex":["女","男"],"count":[31,443]}
     */
    public Map<String, Object> groupBySexBar() {
        List<Map<String, Object>> list = personMapper.groupBySex();
        Map<String, Object> result = new HashMap<>();
        result.put("sex", list.stream().map(m -> m.get("sex")).toArray());
        result.put("count", list.stream().map(m -> m.get("count")).toArray());
        return result;
//        return Collections.emptyMap();
    }

    /**
     * 练习3
     * 按性别分组，返回饼状图 pie 所需数据
     *
     * @return 要求返回的数据格式为 [{"name":"女","value":31},{"name":"男","value":443}]
     */
    public List<Map<String, Object>> groupBySexPie() {
        List<Map<String, Object>> list = personMapper.groupBySex();
        return list.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", m.get("sex"));
            map.put("value", m.get("count"));
            return map;
        }).collect(Collectors.toList());
//        return Collections.emptyList();
    }

    /**
     * 练习4
     * 求武力最高的20名武将的武力和寿命
     *
     * @return 要求返回的数据格式为折线图格式，与柱状图类似：{"name":[...],"forces":[...],"age":[...]}
     */
    public Map<String, Object> showForcesAndAge() {
        List<Map<String, Object>> list = personMapper.showForcesAndAge();
        Map<String, Object> result = new HashMap<>();
        result.put("name", list.stream().map(m -> m.get("name")).toArray());
        result.put("forces", list.stream().map(m -> m.get("forces")).toArray());
        result.put("age", list.stream().map(m -> m.get("age")).toArray());
        return result;
    }

    /**
     * 练习5
     */
    public List<Map<String, Object>> countByLocation1() {
        List<Map<String, Object>> list = personMapper.countByLocation1();
        return list.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", m.get("location"));
            map.put("value", m.get("count"));
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 练习5
     */
    public List<Map<String, Object>> countByLocation2() {
        List<Map<String, Object>> list = personMapper.countByLocation2();
        return list.stream().map(m -> {
            Map<String, Object> map = new HashMap<>();
            map.put("name", m.get("location"));
            map.put("value", m.get("totalcount"));
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 练习6
     */
    public List<Object[]> timeForceAndAge() {
        List<Map<String, Object>> list = personMapper.timeForceAndAge();
        return list.stream().map(m -> new Object[]{formatDate(m.get("birth")), m.get("forces"), m.get("age"), m.get("name")}).collect(Collectors.toList());
    }

    public List<Object[]> maxForceOrAge() {
        List<Map<String, Object>> list = personMapper.maxForceOrAge();
        return list.stream().map(m -> new Object[]{formatDate(m.get("birth")), m.get("forces"), m.get("age"), m.get("name")}).collect(Collectors.toList());
    }

    private static ThreadLocal<SimpleDateFormat> tl = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy"));

    private static long formatDate(Object birth) {
        SimpleDateFormat sdf = tl.get();
        try {
            return sdf.parse(birth.toString()).getTime();
        } catch (ParseException e) {
            return 0;
        }
    }
}
